package managedbean.FRM.CustomerInvoice;

import entity.CRM.Customer;
import entity.CRM.SaleOrder;
import entity.SCM.DeliveryOrder;
import java.io.IOException;
import javax.faces.event.ActionEvent;
import java.io.Serializable;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import session.stateless.CRM.SaleOrderBeanLocal;
import session.stateless.FRM.CustomerInvoiceBeanLocal;

@ManagedBean(name="addCustomerInvoiceManagerBean")
@ViewScoped

public class AddCustomerInvoiceManagerBean implements Serializable {
    
    public AddCustomerInvoiceManagerBean () {
    }
    
    @EJB
    private SaleOrderBeanLocal saleOrderBeanLocal;
    @EJB
    private CustomerInvoiceBeanLocal customerInvoiceBeanLocal;
    
    private DeliveryOrder selectedDeliveryOrder;
    private Customer customer;
    private String statusMessage;
    private boolean statusCashInAdvance;
    private Double finalAmount;
    
    public void initView(PhaseEvent event)
    {        
        if (selectedDeliveryOrder == null)  
        {
            selectedDeliveryOrder = (DeliveryOrder) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("selectedDeliveryOrder");                          
            this.setCustomer(selectedDeliveryOrder.getSaleOrder().getCustomer());
        }
    }
    
    
    public boolean getStatusCashInAdvance()
    {
        SaleOrder saleOrder = this.getSelectedDeliveryOrder().getSaleOrder();
        return saleOrderBeanLocal.havingCashInAdvance(saleOrder.getId());

    }

    public Double getFinalAmount() {
        Double temp_finalAmount;
        if (this.getStatusCashInAdvance())        
            temp_finalAmount = this.finalAmount = selectedDeliveryOrder.getTotalPrice()-selectedDeliveryOrder.getSaleOrder().getCashInAdvance().getCashInAdvanceAmount();            
        else
            temp_finalAmount = this.finalAmount = selectedDeliveryOrder.getTotalPrice();
        return temp_finalAmount;
    }

    public void setFinalAmount(Double finalAmount) {
        this.finalAmount = finalAmount;
    }

    public String getStatusMessage() {
        return statusMessage;
    }

    public void setStatusMessage(String statusMessage) {
        this.statusMessage = statusMessage;
    }

    public DeliveryOrder getSelectedDeliveryOrder() {
        return selectedDeliveryOrder;
    }

    public void setSelectedDeliveryOrder(DeliveryOrder selectedDeliveryOrder) {
        this.selectedDeliveryOrder = selectedDeliveryOrder;
    }

    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }
    
    
    
    public void saveNewCustomerInvoice(ActionEvent event)throws IOException
    {
        System.out.println("test b4 create Invoice"+selectedDeliveryOrder.getTotalPrice()+"  "+finalAmount);
        Long customerInvoiceId =customerInvoiceBeanLocal.createCustomerInvoice(selectedDeliveryOrder.getId(), finalAmount);
        statusMessage = "New Invoice with ID "+customerInvoiceId+" Saved Successfully";
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Add New Customer Invoice Result: " + statusMessage, ""));
        
    }

  

}
